package com.silence.whereeat.main;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.silence.whereeat.R;
import com.silence.whereeat.model.FoodInfo;

import java.util.List;

public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener,
        AdapterView.OnItemClickListener {

    private SwipeRefreshLayout recommendRefresh;
    private int backKeyPressedTime = 0;
    private TextView titleText;
    private ListView recommendList;
    private List<FoodInfo> list;
    private EditText searchEt;
    private int passage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        titleText = (TextView) findViewById(R.id.titleTv);
        titleText.setText("热门食品");

        recommendList = (ListView) findViewById(R.id.recommend_list);
        recommendList.setOnItemClickListener(this);
        recommendRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh_recommend);
        recommendRefresh.setColorSchemeResources(android.R.color.holo_blue_bright);
        recommendRefresh.setOnRefreshListener(this);

        searchEt = (EditText) findViewById(R.id.search_et);
        searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event.getAction() == KeyEvent.KEYCODE_ENTER) {
                    String searchStr = searchEt.getText().toString().trim();
                    search(searchStr);
                }
                return false;
            }
        });

        refresh();
    }

    public void search(String str) {
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                refreshList(list);
            }
        }.execute(str);


    }

    public void loadMore(){
        new AsyncTask<Integer,Void,String>(){

            @Override
            protected String doInBackground(Integer... params) {
                return null;
            }
        }.execute(passage);

        passage++;
    }

    public void refreshList(List<FoodInfo> list) {
        MyFoodAdapter adapter = new MyFoodAdapter(MainActivity.this, R.layout.recommend_item, list);
        recommendList.setAdapter(adapter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && backKeyPressedTime == 0) {
            Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
            backKeyPressedTime = 1;
            new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        backKeyPressedTime = 0;
                    }
                }
            }.start();
            return false;
        } else if (keyCode == KeyEvent.KEYCODE_BACK && backKeyPressedTime == 1) {
            finish();
            System.exit(0);
        }
        return true;
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    public void refresh() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                refreshList(list);
                recommendRefresh.setRefreshing(false);
            }
        }.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
